package com.example.xxpk.service.boxes;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xxpk.mapper.BoxRewardTypesMapper;
import com.example.xxpk.mapper.BoxesMapper;
import com.example.xxpk.mapper.UsersMapper;
import com.example.xxpk.pojo.BoxRewardTypes;
import com.example.xxpk.pojo.Boxes;
import com.example.xxpk.pojo.PurchaseRecord;
import com.example.xxpk.pojo.Users;
import com.example.xxpk.service.purchaserecord.IPurchaseRecordService;
import com.example.xxpk.service.usersignin.UserSignInServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangguihong
 * @since 2025-02-13
 */
@Service
@RequiredArgsConstructor
public class BoxesServiceImpl extends ServiceImpl<BoxesMapper, Boxes> implements IBoxesService {

    private final IPurchaseRecordService purchaseRecordService;

    private final BoxRewardTypesMapper boxRewardTypesMapper;

    private final UsersMapper usersMapper;

    private final UserSignInServiceImpl userSignInService;
    //购买普通宝箱
    @Override
    public boolean addPurchaseBoxes(PurchaseRecord purchaseRecord) {
        LambdaQueryWrapper<PurchaseRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseRecord::getType,2)
                    .ge(PurchaseRecord::getPurchaseTime, LocalDateTime.now().truncatedTo(ChronoUnit.DAYS));
        PurchaseRecord record = purchaseRecordService.getOne(queryWrapper);
        Integer number = record.getNumber();

        LambdaQueryWrapper<Boxes> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Boxes::getType,1);
        Boxes boxes = this.getOne(queryWrapper1);
        if (number >= boxes.getDailyPurchaseLimit()){
            throw new RuntimeException("今日购买数量已达上线");
        }
        return false;
    }

    //购买超级宝箱
    @Override
    public boolean addPurchaseSuperBoxes(PurchaseRecord purchaseRecord) {
        LambdaQueryWrapper<PurchaseRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseRecord::getType,3)
                .ge(PurchaseRecord::getPurchaseTime, LocalDateTime.now().truncatedTo(ChronoUnit.DAYS));
        PurchaseRecord record = purchaseRecordService.getOne(queryWrapper);
        Integer number = record.getNumber();

        LambdaQueryWrapper<Boxes> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Boxes::getType,2);
        Boxes boxes = this.getOne(queryWrapper1);
        if (number >= boxes.getDailyPurchaseLimit()){
            throw new RuntimeException("今日购买数量已达上线");
        }
        return false;
    }

    @Override
    public BoxRewardTypes openBox(Integer userId, Integer type) {

        Users user = usersMapper.selectById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        BoxRewardTypes boxRewardTypes = boxRewardTypesMapper.getRandomRecordByBoxTypeOne(type);
        userSignInService.grantReward(userId, boxRewardTypes.getRewardType(),boxRewardTypes.getRewardCount(),boxRewardTypes.getRewardId());

        return boxRewardTypes;
    }

}
